package voja.android.vwidget;

import java.util.Calendar;
import android.app.PendingIntent;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.text.format.DateFormat;
import android.widget.RemoteViews;

public class VWidgetProvider extends AppWidgetProvider {
	// default values
	public static final String formatTimeDefault = "kk:mm";
	public static final String formatDateDefault = "EEE, dd.MMMM";
	
	private BroadcastReceiver clockReceiver = new BroadcastReceiver(){
		public void onReceive(Context c, Intent intent) {
			RemoteViews v = new RemoteViews(c.getPackageName(), R.layout.widget);
			v.setTextViewText(R.id.txtClock, getTime());
			v.setTextViewText(R.id.txtDate, getDate());
		}
	};
    
	public void onEnabled(Context context){
		Context appContext = context.getApplicationContext();
		appContext.registerReceiver(clockReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
	}
	
	public void onReceive(Context context, Intent intent){
		RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget);
		
		updateWidget(context, v);
		
		context.startService(new Intent(context, ClockService.class));
		
		android.appwidget.AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_IDS), v);
	}
	
	public static void updateWidget(Context context, RemoteViews v) {
		PendingIntent pIntent = PendingIntent.getActivity(context, 0, getAlarmIntent(context), 0);
		v.setOnClickPendingIntent(R.id.txtClock, pIntent);
		
		v.setTextViewText(R.id.txtClock, getTime());
		v.setTextViewText(R.id.txtDate, getDate());
	}
	
	public static Intent getAlarmIntent(Context context){
		String clocks[][] = {
			{ "Standard Alarm", "com.android.alarmclock", "com.android.alarmclock.AlarmClock" },
			{ "HTC Alarm ClockDT", "com.htc.android.worldclock", "com.htc.android.worldclock.WorldClockTabControl" },
			{ "Standard Alarm ClockDT", "com.android.deskclock", "com.android.deskclock.AlarmClock" },
			{ "Froyo Nexus Alarm ClockDT", "com.google.android.deskclock", "com.android.deskclock.DeskClock" },
			{ "Moto Blur Alarm ClockDT", "com.motorola.blur.alarmclock", "com.motorola.blur.alarmclock.AlarmClock" },
			{ "Samsung Galaxy S", "com.sec.android.app.clockpackage", "com.sec.android.app.clockpackage.ClockPackage" } 
		};
		Boolean clockFound = false;
		
		PackageManager packageManager = context.getPackageManager();
		
		Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		ComponentName cn;
		
		for(int i=0; i<clocks.length; i++){
	        String packageName = clocks[i][1];
	        String className = clocks[i][2];
	        
	        try{
	        	cn = new ComponentName(packageName, className);
	        	packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
	        	intent.setComponent(cn);
	        	clockFound = true;
	        	break;
	        }catch(Exception e){
	        }
		}
		if(!clockFound){
        	android.util.Log.e("WidgetProvider", "Error finding AlarmIntent");
		}
		
		return intent;
	}
	
	public static String getTime(){
		return getTime(formatTimeDefault);
	}
	public static String getTime(String format){
		return DateFormat.format(format, Calendar.getInstance()).toString();
	}
	public static String getDate(){
		return getDate(formatDateDefault);
	}
	public static String getDate(String format){
		return DateFormat.format(format, Calendar.getInstance()).toString();
	}
}
